package phidgetmotion.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.text.DecimalFormat;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.phidgets.event.AccelerationChangeEvent;
import com.phidgets.event.AccelerationChangeListener;
import com.phidgets.event.AttachEvent;
import com.phidgets.event.AttachListener;
import com.phidgets.event.DetachEvent;
import com.phidgets.event.DetachListener;
import com.phidgets.event.ErrorEvent;
import com.phidgets.event.ErrorListener;

import phidgetmotion.phidget.Accelerometer;

public class StatusBar extends JPanel {
	private static final long serialVersionUID = -6922968969160768067L;
	private final JLabel connectedLabel = new JLabel();
	private final JLabel xAxisLabel = new JLabel("0,000000");
	private final JLabel yAxisLabel = new JLabel("0,000000");
	private final JLabel zAxisLabel = new JLabel("0,000000");
	private final JLabel errorLabel = new JLabel();
	private AccelerationChangeListener changeListener = new AccelerationChangeListener() {
		@Override
		public void accelerationChanged(AccelerationChangeEvent arg0) {
			String value = new DecimalFormat("#0.000000").format(arg0.getValue());
			switch(arg0.getIndex()){
				case 0:
					xAxisLabel.setText(value);
					break;
				case 1:
					yAxisLabel.setText(value);
					break;
				case 2:
					zAxisLabel.setText(value);
					break;
			}
		}
	};
	private AttachListener attachListener = new AttachListener() {
		@Override
		public void attached(AttachEvent arg0) {
			setConnected(true);
		}
	};
	
	private DetachListener detachListener = new DetachListener() {
		@Override
		public void detached(DetachEvent arg0) {
			setConnected(false);
		}
	};
	
	private ErrorListener errorListener = new ErrorListener() {
		@Override
		public void error(ErrorEvent arg0) {
			errorLabel.setText(arg0.getException().getMessage());
		}
	};
	
	public StatusBar() {
		setPreferredSize(new Dimension(300,25));
		setBorder(BorderFactory.createRaisedBevelBorder());
		setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
		setConnected(Accelerometer.isConnected());
		add(connectedLabel);
		add(Box.createHorizontalStrut(50));
		add(xAxisLabel);
		add(Box.createHorizontalStrut(20));
		add(yAxisLabel);
		add(Box.createHorizontalStrut(20));
		add(zAxisLabel);
		add(Box.createHorizontalStrut(50));
		errorLabel.setForeground(Color.RED);
		add(errorLabel);
		Accelerometer.addAccelerationChangeListener(changeListener);
		Accelerometer.addAttachListener(attachListener);
		Accelerometer.addDetachListener(detachListener);
		Accelerometer.addErrorListener(errorListener);
	}
	
	private void setConnected(boolean connected){
		if(connected){
			connectedLabel.setForeground(Color.GREEN);
			connectedLabel.setText("Connected");
		}
		else{
			connectedLabel.setForeground(Color.RED);
			connectedLabel.setText("Disconnected");
		}
	}
}
